package com.smarttest.common.util.protocol.smart;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Hex;

public class AssemblyPacket {
	
	static List<String> list_7d7e = new ArrayList<String>();
	
	public static String toImei(String s){
		s=s.substring(3,s.length());
		String back="";
		for(int i=0;i<s.length();i++){
			if(i%2==0){
				String tmp=s.substring(i, i+2);
				back=back+" "+tmp;
			}
		}
		back=back.substring(1, back.length());
		return back;
	}
	
	public static String toGpsTime(String s){
		s=s.substring(2,s.length());
		s=s.replace("-", " ");
		String msg_gpstime=s.replace(":", " ");
		return msg_gpstime;
	}
	
	public static String toAcc(int acc){
		String msg_acc="";
		if(0==acc)
			msg_acc="00 03";
		if(1==acc)
			msg_acc="00 02";
		return msg_acc;
	}
	
	public static String toGnnsAcc(int acc){
		String msg_acc="";
		if(0==acc)
			msg_acc="00 00 00 03";
		if(1==acc)
			msg_acc="00 00 00 02";
		return msg_acc;
	}
	
	public static String toSpeed(String s){
		if(0==s.length())
			s="600";
		else if(s.length()>3)
			s=s.substring(0,3)+"0";
		else if(s.length()>0)
			s=s+"0";
		s=string2wordhex(s);
		return s;
	}
	
	public static String toCourse(String s){
		if(0==s.length())
			s="0";
		if(s.length()>3)
			s=s.substring(0,3)+"0";
		s=string2wordhex(s);
		return s;
	}
	
	public static String toLngLat(String s){
		if(s.indexOf(".")!=-1){
			String[] tmp=s.split("\\.");
			int len=tmp[1].length();
			switch(len) 
			{ 
			   case 1: 
			       s=tmp[0]+"."+tmp[1]+"00000"; 
			       break; 
			   case 2: 
				   s=tmp[0]+"."+tmp[1]+"0000";  
			       break; 
			   case 3: 
				   s=tmp[0]+"."+tmp[1]+"000";  
			       break; 
			   case 4: 
				   s=tmp[0]+"."+tmp[1]+"00";  
			       break;
			   case 5: 
				   s=tmp[0]+"."+tmp[1]+"0";  
			       break;
			   default: 
			       break; 
			} 
			s=s.replace(".", "");
			s=stringlnglat2wordhex(s);
		}else{
			if(s.length()>10){
				s=s.substring(0,s.length()-1);
				s=stringlnglat2wordhex(s);
			}else{
				s=stringlnglat2wordhex(s);
			}
				
		}
		return s;
	}
	
	private static String stringlnglat2wordhex(String s){
		String str="";
		if(1==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="00 00 00 0"+Long.toHexString(Long.valueOf(s)).toUpperCase();
		if(2==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="00 00 00 "+Long.toHexString(Long.valueOf(s)).toUpperCase();
		if(3==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="00 00 0"+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(0,1)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(1,3);
		if(4==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="00 00 "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(0,2)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(2,4);
		if(5==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="00 0"+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(0,1)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(1,3)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(3,5);
		if(6==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="00 "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(0,2)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(2,4)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(4,6);
		if(7==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str="0"+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(0,1)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(1,3)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(3,5)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(5,7);
		if(8==Long.toHexString(Long.valueOf(s)).toUpperCase().length())
			str=Long.toHexString(Long.valueOf(s)).toUpperCase().substring(0,2)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(2,4)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(4,6)+" "+Long.toHexString(Long.valueOf(s)).toUpperCase().substring(6,8);
		return str;
	}
	
	public static String string2wordhex(String s){
		String str="";
		if(1==Integer.toHexString(Integer.valueOf(s)).toUpperCase().length())
			str="00 0"+Integer.toHexString(Integer.valueOf(s)).toUpperCase();
		if(2==Integer.toHexString(Integer.valueOf(s)).toUpperCase().length())
			str="00 "+Integer.toHexString(Integer.valueOf(s)).toUpperCase();
		if(3==Integer.toHexString(Integer.valueOf(s)).toUpperCase().length())
			str="0"+Integer.toHexString(Integer.valueOf(s)).toUpperCase().substring(0,1)+" "+Integer.toHexString(Integer.valueOf(s)).toUpperCase().substring(1,3);
		if(4==Integer.toHexString(Integer.valueOf(s)).toUpperCase().length())
			str=Integer.toHexString(Integer.valueOf(s)).toUpperCase().substring(0,2)+" "+Integer.toHexString(Integer.valueOf(s)).toUpperCase().substring(2,4);
		return str;
	}
	
	public static byte[] transfer2Bytes(String input){
		String[] arrs = input.split(" ");
		byte[] bytes = new byte[arrs.length];
		for (int i = 0; i < arrs.length; i++) {
			bytes[i] = (byte)Integer.parseInt(arrs[i],16);
		}
		return bytes;
	}
	
	/*
	 * 将异或过后的报文里的7d、7e进行转义
	 * 
	 */
	public static byte[] edit7d7e(byte[] b){
		b=subBytes(b,1,b.length-2);
		for(int i=0;i<b.length;i++){
			if("125".equals(String.valueOf(b[i]))){
				String str=String.valueOf(i)+","+"7d";
				list_7d7e.add(str);
			}
			if("126".equals(String.valueOf(b[i]))){
				String str=String.valueOf(i)+","+"7e";
				list_7d7e.add(str);
			}
		}
		for(int i=0;i<list_7d7e.size();i++){
			String str[]=list_7d7e.get(i).split(",");
			if(str[1].equals("7d")){
				byte head[]=cutOutByte(b,Integer.valueOf(str[0]));
				int left=(b.length-head.length);
				byte mid[]={(byte)Integer.parseInt("7d",16),(byte)Integer.parseInt("01",16)};
				byte tail[]=subBytes(b,Integer.valueOf(str[0])+1,left-1);
				b=getMergeBytes(head,mid);
				b=getMergeBytes(b,tail);
				if(i==list_7d7e.size()-1)
					break;
				for(int j=(i+1);j<list_7d7e.size();j++){
					String next_str[]=list_7d7e.get(j).split(",");
					int new_index=Integer.valueOf(next_str[0])+1;
					list_7d7e.set(j, new_index+","+next_str[1]);
				}
				
			}
			if(str[1].equals("7e")){
				byte head[]=cutOutByte(b,Integer.valueOf(str[0]));
				int left=(b.length-head.length);
				byte mid[]={(byte)Integer.parseInt("7d",16),(byte)Integer.parseInt("02",16)};
				byte tail[]=subBytes(b,Integer.valueOf(str[0])+1,left-1);
				b=getMergeBytes(head,mid);
				b=getMergeBytes(b,tail);
				if(i==list_7d7e.size()-1)
					break;
				for(int j=(i+1);j<list_7d7e.size();j++){
					String next_str[]=list_7d7e.get(j).split(",");
					int new_index=Integer.valueOf(next_str[0])+1;
					list_7d7e.set(j, new_index+","+next_str[1]);
				}
			}
		}
		byte tmp[]={(byte)Integer.parseInt("7e",16)};
		b=getMergeBytes(tmp,b);
		b=getMergeBytes(b,tmp);
		list_7d7e.clear();
		return b;
	}
	
	public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i=begin; i<begin+count; i++) bs[i-begin] = src[i];
        return bs;
    }
	/**
	 * 截取byte数据
	 * @param b	是byte数组
	 * @param j	是大小
	 * @return
	 */
	public static byte[] cutOutByte(byte[] b,int j){
		if(b.length==0 || j==0){
			return null;
		}
		byte[] bjq = new byte[j];
		for(int i = 0; i<j;i++){
			bjq[i]=b[i];
		}
		return bjq;
	}
	
	/**
	 * 合并两个byte数组
	 * @param pByteA
	 * @param pByteB
	 * @return
	 */
	public static byte[] getMergeBytes(byte[] pByteA, byte[] pByteB){
		int aCount = pByteA.length;
		int bCount = pByteB.length;
		byte[] b = new byte[aCount + bCount];
		for(int i=0;i<aCount;i++){
			b[i] = pByteA[i];
		}
		for(int i=0;i<bCount;i++){
			b[aCount + i] = pByteB[i];
		}
		return b;
	}
	
	public static String byteToString(byte[] b){
		char[] chArr = Hex.encodeHex(b);
		String str="";
		for (char c : chArr) {
			str+=String.valueOf(c).toUpperCase();
		}
		String respone="";
		for(int i=0;i<str.length();i++){
			if(i%2==0){
				String tmp=str.substring(i,i+2);
				respone=respone+" "+tmp;
			}
		}
		respone=respone.substring(1,respone.length());
		return respone;
	}
	
}
